package com.augurit.gzcgwsp.ljzyz.web;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnBusinfoHfqk;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoHfqkService;
import com.augurit.gzcgwsp.ljzyz.service.IBusIccardManageService;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;
import com.augurit.gzcgwsp.wfex.common.util.WfUtilsEX;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;
import com.augurit.wf.common.constant.WfConstant;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-yn-businfo.action",type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/iccardManage/listBusinfo.jsp"),
	@Result(name = "printview", location = "/gzcgwsp/iccardManage/printview.jsp"),
	@Result(name = "printAllView", location = "/gzcgwsp/iccardManage/printAllView.jsp"),
	@Result(name = "printcz", location = "/gzcgwsp/iccardManage/printdoc.jsp"),
	@Result(name = "printCard", location = "/gzcgwsp/wfex/client/wf-printExcelEx.jsp"),
	@Result(name = "viewCard", location = "/gzcgwsp/iccardManage/viewCard.jsp")
})
public class BusIccardManageAction extends CrudActionSupport<YwYnBusinfoForm>{

	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
	@Autowired
	private IBusIccardManageService busIccardManageService;
	@Autowired
	private IYwYnBusinfoHfqkService ywYnBusinfoHfqkService;
	private YwYnBusinfoForm form;
	private Long id;
	private Page<YwYnBusinfoForm> page;

	public Page<YwYnBusinfoForm> getPage() {
		return page;
	}

	public void setPage(Page<YwYnBusinfoForm> page) {
		this.page = page;
	}

	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		String flag=getRequest().getParameter("flag");//表示是否要读ic卡
		List<YwComQyxx> qyxx=ywYnBusinfoService.getQyxx();
		if(form.getQyid()!=null){
			busIccardManageService.search(page, form);	
		}
		getRequest().setAttribute("flag", flag);
		getRequest().setAttribute("qyxx", qyxx);
		return SUCCESS;
	}
	/**
	 * 分页统计查询
	 * @return
	 */
    public String toList(){
	    if(form.getQyid()!=null){
	    	if(page==null){
				 page= new Page<YwYnBusinfoForm>(15);
				 ExtUtils.initPageFromExtGridParam(getRequest(), page);
			}
			busIccardManageService.search(page, form);	
			//renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
			JSONArray memoObject = JSONArray.fromObject(page.getResult());
			String jsonString = "{start:" + page.getFirst() + ",limit:" + page.getPageSize()
					+ ",totalProperty:" + page.getTotalCount() + ",root:"
					+ memoObject.toString() + "}";
			Struts2Utils.renderHtml(jsonString);
		}
	  	
	  	return null;
  }
    
    /**
     * 发卡
     * @return
     */
   public String writeCard(){
	   boolean flag=busIccardManageService.sendIccard(form.getId(),this.getLoginUser());
		try {
			this.getResponse().getWriter().print(flag);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
	/**
	 * 获取登录用户的呈批业务,用于查询待办已办的查询
	 * @return
	 */
	public String getCompany(){
		List<YwComQyxx> list=ywYnBusinfoService.getQyxx();
    	 List<BasicCombo> combos = new ArrayList<BasicCombo>();
    	 if(list!=null && list.size()>0){
    		 for (int i=0;i<list.size();i++) {
    			 YwComQyxx form=(YwComQyxx) list.get(i);
    			 Long id=form.getId();
    			 String name=form.getSqdw();
 				BasicCombo combo = new BasicCombo();
 				combo.setLabel(name);
 				combo.setValue(String.valueOf(id));
 				combos.add(combo);
 		 }
    	 }
		
		 ExtBaseAction.extRenderListJson(combos, true);
		 return null;
	}
	
	/**
	 * 到ic卡读取数据页面
	 * @return
	 * @throws Exception
	 */
	public String readCard() throws Exception {
		return "viewCard";
	}
	
	/**
	 * 读取ic卡的信息返回
	 * @return
	 * @throws Exception
	 */
	public String readCardInfo() throws Exception {
		form=busIccardManageService.readBusInfo();
		//form=ywYnBusinfoService.getBusByNo("粤A72022");
		//String yxsj=ywYnBusinfoService.getZyzyxqByBusId(form.getId());
		String yxsj="";
		if(form!=null){
			if(form.getYxjsrq()!=null){
				yxsj=DateUtilsEX.dateFormate(form.getYxjsrq(), "yyyy-MM-dd");
				yxsj+="~"+DateUtilsEX.dateFormate(form.getYxjsrq(), "yyyy-MM-dd");
				
			}
			Map map=WfUtilsEX.buildMap(form,"yyyy-MM-dd");
			map.put("yxsj", yxsj);
			renderJson(map);
		}else{
			renderJson("");
		}
		return null;
	}
	
	/**
	 * 清除ic卡信息
	 * @return
	 * @throws Exception
	 */
	public String  delCard() throws Exception{
		boolean flag=busIccardManageService.delCard();
		if(flag){
			getResponse().getWriter().print("success");
		}
		return null;
	}
	/**
	 * 修改有效期
	 * @return
	 * @throws Exception
	 */
	public String  updateCard() throws Exception{
		
		boolean flag=busIccardManageService.updateCard(form.getId());
		if(flag){
			getResponse().getWriter().print("success");
		}
		return null;
	}
	
	/**
	 * 打印预览
	 * @return
	 * @throws Exception
	 */
	public String  printViewCard() throws Exception{
		 String flag=getRequest().getParameter("flag");
		 String yxsj=ywYnBusinfoService.getZyzyxqByBusId(form.getId());
		 form.setZyzyxqStr(yxsj);
		 getRequest().setAttribute("yxsj", yxsj);
		 if("new".equals(flag)){//表示没有打印过卡
			 return "printAllView";
		 }
		 return "printview";
	}
	
	
	/**
	 * 打卡
	 * @return
	 * @throws Exception
	 */
	public String  printCard() throws Exception{
		 String tempPath="Templates/ynpf/iccard.xls";
        String context=getRequest().getSession().getServletContext().getRealPath("");
        String inpath=context+File.separator+tempPath;
        tempPath=busIccardManageService.writeData(form,context,inpath);
        getRequest().setAttribute("templateFilePath",tempPath);
        logger.info("返回文件路径"+tempPath);
        return "printCard";
	}
	
	/**
	 * 用word模版打印
	 * @return
	 * @throws Exception
	 */
    public String printcz() throws Exception {
    	String printrow=getRequest().getParameter("printrow");
    	String gdjrzh=getRequest().getParameter("gdjrzh");
    	String qydj=getRequest().getParameter("qydj");
    	//工地进入证号保存到审核表
    	this.busIccardManageService.saveInfo(gdjrzh,qydj,form.getId());
    	String yxrq=ywYnBusinfoService.getZyzyxqByBusId(form.getId());
    	String[] rqs=yxrq.split("~");
    	Date start=DateUtilsEX.getStringToDate(rqs[0], "yyyy-MM-dd");
    	Date end=DateUtilsEX.getStringToDate(rqs[1], "yyyy-MM-dd");
    	yxrq=DateUtilsEX.dateFormate(start, "yyyy年MM月dd日")+"至"+DateUtilsEX.dateFormate(end, "yyyy年MM月dd日");
    	
    	Map map=WfUtilsEX.buildMap(form, "yyyy年MM月dd日");
    	String tempFile="Templates/ynpf/iccard.doc";
    	if("1".equals(printrow)){
    		map.put("yxrq", yxrq);
    	}else{	
    		tempFile="Templates/ynpf/iccard1.doc";
    		map.put("yxrq"+printrow, yxrq);
    	}
    	if(StringUtils.isNotBlank(gdjrzh)){
    		map.put("gdjrzh", gdjrzh);
    	}
    	String qydjstr=qydj==null?"B级":qydj;
    	map.put("qydj", qydjstr);
    	getRequest().setAttribute(WfConstant.PRINT_BUFFER, map);
    	getRequest().setAttribute("templateFilePath", tempFile);
        return "printcz";
    }
    
	
	/**
	 * 打印有效日期
	 * @return
	 * @throws Exception
	 */
	public String  printCardDate() throws Exception{
		//form.setId(Long.valueOf(busId));
		String tempPath="Templates/ynpf/iccard1.xls";
        String context=getRequest().getSession().getServletContext().getRealPath("");
        String inpath=context+File.separator+tempPath;
        tempPath=busIccardManageService.printDate(form,context,inpath);
        getRequest().setAttribute("templateFilePath",tempPath);
        logger.info("返回文件路径"+tempPath);
        return "printCard";
	}
	
	
	/**
	 * 发卡
	 * @return
	 * @throws Exception
	 */
	public String sendIccard()throws Exception {
		boolean flag=busIccardManageService.sendIccard(form.getId(),this.getLoginUser());
		 getRequest().setAttribute("flag",flag);
		return null;
	}
	
	/**
	 * 批量发卡
	 * @return
	 * @throws Exception
	 */
	public String sendAllCard()throws Exception {
		String ids=this.getRequest().getParameter("ids");
		String[] idarr=ids.split(",");
		for(String id:idarr){
			busIccardManageService.sendIccard(Long.valueOf(id),this.getLoginUser());
		}
		
		return null;
	}
	
	
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String deleteMore() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = this.ywYnBusinfoService.get(id);
			YwYnBusinfoHfqk hfqk=ywYnBusinfoHfqkService.getNewHfqkByBusId(id);
			if(hfqk!=null){
				form.setGdjrzh(hfqk.getGdjrzh());
			}
		}
		else{
			form = new YwYnBusinfoForm();
		}
		
		
	}



	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public YwYnBusinfoForm getModel() {
		return form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


}
